import java.io.{FileOutputStream, OutputStream, PrintWriter}

import scala.collection.mutable.ArrayBuffer
import scala.io.Source

/**
  * @Author Mr.An
  * @Date 18/4/17 下午3:53
  *first split file should remove first title line.
  */
object TestReplace {
  def main(args: Array[String]): Unit = {
    def testFirst(filename:String): Unit ={
      val lines=Source.fromFile("/Users/finup/Desktop/"+filename+".csv","gbk").getLines();
      var i=0;
      val newAry=new ArrayBuffer[String]();
      while (lines.hasNext){
        val line=lines.next();
        val newline=line.replace("\"","");
        if(i!=0){
          newAry.append(newline)
        }
        i+=1;
      }
      val file="/Users/finup/Desktop/"+filename+"_m.csv";

      val writer=new PrintWriter(file,"gb2312");

      writer.write(newAry.mkString("\n"));
      writer.flush();
    }

    def split2ten(fileName:String): Unit ={
      val arys=Source.fromFile("/Users/finup/Desktop/"+fileName+".csv","gbk").getLines().toArray;
      val dstfilePath="/Users/finup/Desktop/"+fileName+"_split"
      var i=0;
      val size=arys.size
      println(size+":::arys size")
      val splitSize=size/10;
      for(i <- 0 until 10){
        var temp=Array.apply("aaa");
        if(i<9&&i>0){
          temp=new Array[String](splitSize);
          Array.copy(arys,0+(i*splitSize),temp,0,temp.length)
        }else if(i==0){
          temp=new Array[String](splitSize-1);
          Array.copy(arys,1+(i*splitSize),temp,0,temp.length)   //remove first split's first title line
        }else{
          temp=new Array[String](size-i*splitSize);
          Array.copy(arys,0+(i*splitSize),temp,0,temp.length)
        }

        val dstTempFile=dstfilePath+i.toString+".csv"
        val tempwriter=new PrintWriter(dstTempFile,"gb2312");
        tempwriter.write(temp.mkString("\n").replaceAll("\"",""))
        tempwriter.flush()
      }


      }

      def split2(fileName:String): Unit ={
        val arys=Source.fromFile("/Users/finup/Desktop/"+fileName+".csv","gbk").getLines().toArray;
        val dstfilePath="/Users/finup/Desktop/"+fileName+"_split"
        var i=0;
        val size=arys.size
        println(size+":::arys size")
        val splitSize=size/2;
        for(i <- 0 until 2){
          var temp=Array.apply("aaa");
          if(i<1){
            temp=new Array[String](splitSize-1);
            Array.copy(arys,1+(i*splitSize),temp,0,temp.length)  //remove first split's first title line
          }else{
            temp=new Array[String](size-i*splitSize);
            Array.copy(arys,0+(i*splitSize),temp,0,temp.length)
          }
          val dstTempFile=dstfilePath+i.toString+".csv"
          val tempwriter=new PrintWriter(dstTempFile,"gb2312");
          tempwriter.write(temp.mkString("\n").replaceAll("\"",""))
          tempwriter.flush()
        }



    }
//    testFirst("gongxiangpingtai201804173yue");
//      split2ten("gongxiangpingtai201804173yue")
      split2("gongxiangpingtai201804173yue")
  }

}
